// arcOscTranslator
// Created by JP Sykes - Nomeist.com
//
// translates standard arc osc communication so it works with the arcTouch template
// to be used for development purposes
//


// inlets and outlets
inlets = 1;
outlets = 1;

var outputString;
// Stick this in here so it works with none Max text page.
if(!outlet){
  function outlet(num, strng){
    document.write("<p>"+strng+"</p>");
  }
}

// Utility function for working with the 4x16 multislider
// this function coverts 0-63 into a coordinate on those sliders

function makeMultiSlider(val){
  var msCoord = {
    "multislider": Math.floor(val / 16),
    "subSlider": (Math.floor(val % 16))+1
  }
  return msCoord
}

function pausecomp(millis){
var date = new Date();
var curDate = null;

do { curDate = new Date(); } 
while(curDate-date < millis);
}


// all the translation functions

function arcRingSet(arc, led, intensity){
  var ledCoords = makeMultiSlider(led);
  //outlet(0,"/1/arc"+arc+"led"+ledCoords['multislider']+"/"+ledCoords['subSlider']+" "+intensity);
  outlet(0,"/1/arc"+arc+"led"+ledCoords['multislider']+"/"+ledCoords['subSlider'], intensity);
}



function arcRingAll(arc, intensity){
  var j=16; // 0-64 leds
  var l = 3;
  for (k=0; k<=l; k++){    
    for (i=0;i<=j;i++){
      post(i);
      //var ledCoords = makeMultiSlider(i);
      // k=k+1;
      // i=i+1;
      outlet(0,"/1/arc"+arc+"led"+k+"/"+i, intensity);
      pausecomp(10);
    }
  }
}

function arcRingMap(arc, ledArray){
   var j=ledArray.length-1;
   for (i=0;i<=j;i++){
     var k = ledArray[i];
     var ledCoords = makeMultiSlider(i);
     outlet(0,"/1/arc"+arc+"led"+ledCoords['multislider']+"/"+ledCoords['subSlider'], k);
   }
   
}

function arcRingRange(arc, start, end, intensity){
  // is the end smaller than the start
  // in which case we have a wrap around
  if(start > end){
    // take it from start to 63
    for (i=start;i<=63;i++){
      var ledCoords = makeMultiSlider(i);
      outlet(0,"/1/arc"+arc+"led"+ledCoords['multislider']+"/"+ledCoords['subSlider'], intensity);
    }
    // now start at 0 and go up to end
    for (j=0;j<=end-1;j++){
      var ledCoords = makeMultiSlider(j);
      outlet(0,"/1/arc"+arc+"led"+ledCoords['multislider']+"/"+ledCoords['subSlider'], intensity);
    }
    
  }else{
    // just loop from start to end
    for (i=start;i<=end;i++){
      var ledCoords = makeMultiSlider(i);
      outlet(0,"/1/arc"+arc+"led"+ledCoords['multislider']+"/"+ledCoords['subSlider'], intensity);
    }
    
  }
  
}

// bang -- utility stuff
function bang(){
  outlet(0, outputString)
  post(outputString);
}